define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'appointment/index' + location.search,
                    add_url: 'appointment/add',
                    edit_url: 'appointment/edit',
                    del_url: 'appointment/del',
                    multi_url: 'appointment/multi',
                    table: 'appointment',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.username', title: __('User.username')},
                        // {field: 'user.mobile', title: __('User.mobile')},
                        {field: 'user_name', title: __('User_name')},
                        {field: 'shop.name', title: __('Shop.name')},
                        {field: 'house.name', title: __('House.name')},
                        {field: 'adviser', title: "顾问"},
                        {field: 'technician_text', title: __('Technician.name')},
                        {field: 'project', title: __('Project.name'), formatter: function(value, row){
                            var projectArr = value.split(',');
                            var str = '';
                            for (var i = 0; i < projectArr.length; i++) {
                                str += '<span class="label label-warning" style="margin-left:5px">'+projectArr[i]+'</span>'
                                // console.log(projectArr[i])
                            }
                            return str;

                        }},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'appointment_time', title: __('Appointment_time'), operate:'RANGE', addclass:'datetimerange'},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons:[
                            {
                                name: 'kaidan',
                                title: __('开单'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'operation/kaidan'
                            },
                            {
                                name: 'dispensing',
                                title: __('顾问配单'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'operationproject/index'
                            }
                        ]}
                    ] 
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});